package com.geely.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 占助
 * @date 2020/7/14 11:19
 * @description ${手动确认消息监听类}
 * @Copyright © 吉利汽车
 * @Version: 1.0
 */
@Component
public class MyAckReceiver implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //因为传递消息的时候是用map传递，所以将Map从Message内取出需要做些处理
            System.out.println(message.getBody());
            System.out.println(new String(message.getBody()));
            System.out.println("===============================>");
//            String msg = message.toString();
//            String[] msgArray = msg.split("'");//可以点进Message里面看源码,单引号直接的数据就是我们的map消息数据
//            Map<String, String> msgMap = mapStringToMap(msgArray[1].trim());
//            String messageId=msgMap.get("messageId");
//            String messageData=msgMap.get("messageData");
//            String createTime=msgMap.get("createTime");
//
//            if ("TestDirectQueue".equals(message.getMessageProperties().getConsumerQueue())){
//                System.out.println("消费的消息来自的队列名为："+message.getMessageProperties().getConsumerQueue());
//                System.out.println("消息成功消费到  messageId:"+messageId+"  messageData:"+messageData+"  createTime:"+createTime);
//                System.out.println("执行TestDirectQueue中的消息的业务处理流程......");
//
//            }
//            if ("fanout.A".equals(message.getMessageProperties().getConsumerQueue())){
//                System.out.println("消费的消息来自的队列名为："+message.getMessageProperties().getConsumerQueue());
//                System.out.println("消息成功消费到  messageId:"+messageId+"  messageData:"+messageData+"  createTime:"+createTime);
//                System.out.println("执行fanout.A中的消息的业务处理流程......");
//
//            }
//            channel.basicQos(1);
//            channel.basicAck(deliveryTag,true); // 为true 会重新放回队列
            channel.basicNack(deliveryTag, false, true);
        }catch (Exception e){
            channel.basicReject(deliveryTag,false);
            e.printStackTrace();
        }
    }

    //{key=value,key=value,key=value} 格式转换成map
    private Map<String,String> mapStringToMap(String str){
        str = str.substring(1,str.length() - 1);
        String[] strs = str.split(",");
        Map<String, String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key = string.split("=")[0].trim();
            String value = string.split("=")[1];
            map.put(key, value);
        }
        return  map;
    }
}
